const models = require('./db')
const express = require('express')
const router = express.Router()
const mysql = require('mysql')
const $sql = require('./splMap')
const {
    json
} = require('body-parser')

// 连接数据库
const conn = mysql.createConnection(models.mysql)
conn.connect()


const jsonWrite = function (res, ret) {
    if (typeof ret === 'undefined') {
        res.json({
            code: '1',
            msg: '操作失败'
        })
    } else {
        console.log('success！！！');
        res.json(
            ret
        )
    }
}

//展示用户信息
router.get('/showalluser', (req, res) => {
    const sql = $sql.showalluser
    const params = req.body
    console.log('添加', params, sql)
    conn.query(sql, function (err, result) {
        if (err) {
            console.log(err)
        }
        if (result) {
            jsonWrite(res, result)
        }
    })
})

//增加商品信息
router.post('/addgoods', (req, res) => {
    const sql = $sql.addgoods
    const params = req.body
    console.log('添加', params, sql)
    conn.query(sql, params, function (err, result) {
        if (err) {
            console.log(err)
        }
        if (result) {
            console.log('操作成功')
            jsonWrite(res, result)
        }
    })
})

//展示商品信息
router.get('/showallgoods', (req, res) => {
    const sql = $sql.showallgoods
    const params = req.body
    console.log('添加', params, sql)
    conn.query(sql, function (err, result) {
        if (err) {
            console.log(err)
        }
        if (result) {
            jsonWrite(res, result)
        }
    })
})

//过滤商品信息
router.post('/filtergoods', (req, res) => {
    const sql = $sql.filtergoods
    const params = req.body
    console.log('添加', params, sql)
    conn.query(sql, [params.leixing], function (err, result) {
        if (err) {
            console.log(err)
        }
        if (result) {
            jsonWrite(res, result)
        }
    })
})

//删除商品信息
router.post('/deletegoods', (req, res) => {
    const sql = $sql.deletegoods
    const params = req.body
    console.log('添加', params, sql)
    conn.query(sql, [params.iid, params.leixing], function (err, result) {
        if (err) {
            console.log(err)
        }
        if (result) {
            jsonWrite(res, result)
        }
    })
})

//更新商品信息
router.post('/updategoods', (req, res) => {
    const sql = $sql.updategoods
    const params = req.body
    console.log('添加', params, sql)
    conn.query(sql, [params, params.iid], function (err, result) {
        if (err) {
            console.log(err)
        }
        if (result) {
            console.log('操作成功')
            jsonWrite(res, result)
        }
    })
})

// 搜索商品信息
router.post('/searchgoods', (req, res) => {
    const sql = $sql.searchgoods
    const params = req.body
    console.log('添加', params, sql)
    var t = '%' + params.title + '%'
    conn.query(sql, [t], function (err, result) {
        if (err) {
            console.log(err)
        }
        if (result) {
            console.log('操作成功')
            jsonWrite(res, result)
        }
    })
})





module.exports = router